@include('Admin.public.head')
<body>
@include('Admin.public.menu')
<div class=" admin-content">
  <div class="daohang">

</div>
<div class="admin-biaogelist" id="appUser">
    <div class="listbiaoti am-cf">
      <ul class="am-icon-flag on"> 添加管理员</ul>
      {{--<dl class="am-icon-home" style="float: right;"> 当前位置： 首页 > <a href="#">商品列表</a></dl>--}}
    </div>
    <div class="fbneirong" >
      <form class="am-form">
        {{ csrf_field() }}
        <div class="am-form-group am-cf">
          <div class="zuo">用户名：</div>
          <div class="you">
            <input type="text" class="am-form-field" id="doc-ipt-email-1" v-model="users.name" @blur="check()" placeholder="请输入用户名" required>
          </div>
        </div>
        <div class="am-form-group am-cf">
          <div class="zuo">密码：</div>
          <div class="you">
            <input type="password" class="am-form-field" v-model="users.password" placeholder="默认密码和用户名一致">
          </div>
        </div>
        <div class="am-form-group am-cf">
          <div class="zuo">姓名：</div>
          <div class="you">
            <input type="text" class="am-form-field" v-model="users.user_name" placeholder="请输入姓名" required>
          </div>
        </div>
        <div class="am-form-group am-cf">
          <div class="zuo">电话：</div>
          <div class="you">
            <input type="number" class="am-form-field" maxlength="11"  v-model="users.phone" placeholder="请输入电话">
          </div>
        </div>
        <div class="am-form-group am-cf">
          <div class="zuo">邮箱：</div>
          <div class="you">
            <input type="text" class="am-form-field" v-model="users.emil" placeholder="请输入邮箱" required>
          </div>
        </div>
        <div class="am-form-group am-cf">
          <div class="zuo">职位：</div>
          <div class="you">
            <input type="text" class="am-form-field" id="doc-ipt-email-1" v-model="users.position" placeholder="请输入职位" required>
          </div>
        </div>
        <div class="am-form-group am-cf">
          <div class="zuo">公司类别：</div>
          <div class="am-btn-group am-btn-group-xs">
            <select style="width: 200px" v-model="users.is_branch">
              <option value="0">总公司</option>
              <option v-for="val in branch" :value="val.id">@{{val.name}}</option>
            </select>
          </div>
          <a href="/admin/system/branchPage">添加公司</a>
        </div>
        <div class="am-form-group am-cf" v-show="users.is_branch == 0">
          <div class="zuo">后台权限：</div>
          <div class="you">
            <div class="am-form-group">
              <div v-for="val in permissions">
                <p style="padding: 10px">@{{ val.name }}</p>
                <label class="am-checkbox-inline" v-for="v in val.children">
                  <input type="checkbox" v-model="v.status" data-am-ucheck> @{{ v.name }}
                </label>
              </div>
            </div>
          </div>
        </div>
        <div class="am-form-group am-cf">
          <div class="zuo">其他信息：</div>
          <div class="you">
            <textarea class="" rows="2"  v-model="users.note" id="doc-ta-1"></textarea>
          </div>
        </div>
        <div class="am-form-group am-cf">
          <div class="you" style="margin-left: 11%;">
            <button type="button" class="am-btn am-btn-success am-radius" v-on:click="ensure()">确 定</button>&nbsp;&nbsp;
            <button type="button" class="am-btn am-btn-secondary am-radius" onclick="javascript:history.back(-1);">返 回</button>
          </div>
        </div>
      </form>
    </div>
  <script>
      var app = new Vue({
          el:"#appUser",
          data: {
              permissions : [], //权限
              branch:[],// 分公司列表
              users:{
                  name:'',
                  user_name:'',
                  emil:'',
                  phone:'',
                  password:'',
                  position:'',
                  is_branch:0,
                  note:'',
              },
          },
          //事件
          methods: {
              //初始化 系统管理员列表
              initialize:function () {
                  //编辑
                  var id = this.obtain('id');
                  $.get("/admin/system/getPermissions",{class:'select',user_id:id},function (res) {
                      if(res.code){
                          app.permissions = res.data
                      } else {
                          layer.msg(res.data);
                      }
                  });
                  $.get("/admin/system/getBranch",{class:'select'},function (res) {
                      if(res.code){
                          app.branch = res.data
                      } else {
                          layer.msg(res.data);
                      }
                  });

                  if(id){
                      $.post("/admin/system/user",{'_token':'{{csrf_token()}}',id:id,class:'edit'},function (res) {
                          if(res.code){
                              app.users = res.data
                          } else {
                              layer.msg(res.data);
                          }
                      });
                  }
              },
              //确定
              ensure:function () {
                  if (app.users.name == '') {
                      layer.msg('用户名不能为空');return false;
                  }
                  if (app.users.password == '') {
                      layer.msg('密码不能为空');return false;
                  }
                  if (app.users.user_name == '') {
                      layer.msg('姓名不能为空');return false;
                  }
                  if (app.users.phone == '') {
                      layer.msg('电话不能为空');return false;
                  }
                  // console.dir(app.permissions)
                  // if(app.users.is_branch == 0){
                  //     console.dir(app.permissions.length)
                  //     for(var i=0;app.permissions.length>i;++i){
                  //         console.dir(app.permissions[i]);
                  //         var children = app.permissions[i];
                  //         for (var j=0;children.length>j;++j){
                  //             console.dir(children[j]);
                  //         }
                  //     }
                  // }
                  $.post("/admin/system/user",{'_token':'{{csrf_token()}}',class:'add',users:app.users,permissions:app.permissions},function (res) {
                      if(res.code == 0){
                          layer.msg(res.data);
                      } else {
                          layer.msg(res.data,function () {
                              window.location.href="/admin/system/index";
                          },1000);
                      }
                  });
              },
              //查看用户名
              check:function () {
                  if (app.users.name == '') {
                      layer.msg('用户名不能为空');return false;
                  }
                  $.post("/admin/system/user",{'_token':'{{csrf_token()}}',class:'find',name:app.users.name},function (res) {
                      if(res.code == 0){
                          layer.msg(app.users.name+res.data);
                      } else {
                          app.users.password = app.users.name;
                      }
                  });
              },
              //获取url id
              obtain:function (val) {
                  var query = window.location.search.substring(1);
                  var vars = query.split("&");
                  for (var i=0;i<vars.length;i++) {
                      var pair = vars[i].split("=");
                      if(pair[0] == val){
                          return pair[1];
                      }
                  }
              },
          },
          //自动执行
          mounted: function () {
              this.initialize();//分公司
          },
      })
  </script>
@include('Admin.public.foot')